from django.contrib.auth.forms import UserCreationForm, UserChangeForm, AuthenticationForm
from django import forms
from .models import CustomerUser

class UserRegisterForm(UserCreationForm):
    class Meta:
        model = CustomerUser
        fields = ['username', 'password1', 'password2']

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fields['username'].help_text = '150字符或更少。仅包含字母、数字和 @/./+/-/_。'
        self.fields['password1'].help_text = '密码长度至少8位，不能全是数字。'
        self.fields['username'].widget.attrs.update({'class': 'form-control'})
        self.fields['password1'].widget.attrs.update({'class': 'form-control'})
        self.fields['password2'].widget.attrs.update({'class': 'form-control'})
class UserLoginForm(AuthenticationForm):
    # test = forms.CharField(max_length=100)
    remember_me = forms.BooleanField(
        required=False,
        initial=True,
        widget=forms.CheckboxInput(attrs={'class': 'form-check-input'})
    )
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)  # 调用父类的init方法，防止父类初始化错误
        self.fields['username'].widget.attrs.update({'placeholder': '用户名','class': 'form-control'})
        self.fields['password'].widget.attrs.update({'placeholder': '密码','class': 'form-control'})
        # self.fields就是定义的,就如上面那个，通过self.fields['test']调用
# class Parent:
#     def __init__(self, name):
#         self.name = name
# class Child(Parent):
#     def __init__(self, name, age):
#         super().__init__(name)   # 调用父类初始化name
#         self.age = age           # 子类额外初始化age

class UserInfoChange(UserChangeForm):
    password = None
    class Meta:
        model = CustomerUser
        fields = ['username', 'profile_pic', 'bio','deepseekapi']
    def __init__(self,*args,**kwargs):
        super().__init__(*args, **kwargs)
        self.fields['username'].disabled = True  # 禁止用户修改用户名（但需要显示原始值）
        self.fields['username'].widget.attrs.update({'class': 'form-control'})
        self.fields['profile_pic'].widget.attrs.update({'class': 'form-control'})
        self.fields['bio'].widget.attrs.update({'class': 'form-control'})
        self.fields['deepseekapi'].widget.attrs.update({'class': 'form-control'})